// @ts-nocheck
import DialogVue from "./dialog.vue";
import { Popup } from "../popup";
import { DialogOptions } from "./type";
import { VNode } from "vue";
import { PopupEvent } from "../type";
import { ButtonGroup } from "../../button-group";
import { BasicFormOptions } from "../../basic-form-box/type";
import { DataQueryDetailPage, LifeOption } from "../../data-query-detail";
import { RequestOptions } from "../../data-query-detail/types";
import { TableOptions } from "../..";

type NameType = Record<string, any>;

export type InitPageType<F extends NameType = NameType, T extends NameType = NameType> = ReturnType<
	typeof createPDG<F, T>
>;

/** 创建页面模块 */
function createPDG<FN extends NameType, TN extends NameType>(option: LifeOption = {}) {
	class PDG<FN extends NameType, TN extends NameType> extends DataQueryDetailPage<FN, TN> {
		createForm<K extends keyof FN>(key: K, opt: Partial<BasicFormOptions<FN[K]>> = {}) {
			return super.initForm(key, opt);
		}

		createTable<K extends keyof TN>(
			key: K,
			/** 请求信息配置 */
			reqOpt: RequestOptions<TN[K]> = {},
			/** 表格信息配置 */
			opt: Partial<TableOptions<TN[K]>> = {}
		) {
			return super.initTable(key, reqOpt, opt);
		}
	}

	return new PDG<FN, TN>("_PDG__", option, true);
}

class DialogPage extends Popup<DialogOptions> {
	private buttonGroup!: ButtonGroup;
	private page!: InitPageType;

	constructor() {
		super(DialogVue);
	}

	initPage<FN extends NameType, TN extends NameType>(
		callback: (p: InitPageType<FN, TN>) => void,
		opt: LifeOption = {}
	) {
		this.page = createPDG<FN, TN>(opt) as any;
		callback && callback(this.page as any);
		return this;
	}

	initButton<FN extends NameType, TN extends NameType>(
		callback: (btn: ButtonGroup, page: InitPageType<FN, TN>) => void
	) {
		this.buttonGroup = new ButtonGroup();
		callback && callback(this.buttonGroup, this.page as any);
		return this;
	}
	/**
	 * 打开弹出层
	 * @param opts 弹出层配置
	 * @returns
	 */
	open(opts: DialogOptions): Promise<PopupEvent> {
		// !todo 此处两次调用会导致默认值被重置
		this.showPopup(opts, true);
		return Promise.resolve().then(() => this.showPopup({ ...opts }));
	}
	/**
	 * 获取中间内容的VNode
	 * @returns
	 */
	getContentVNode(): VNode {
		return this.page.getVNode();
	}
	/**
	 * 获取底部按钮组VNode
	 */
	getBottomVNode(): VNode {
		return this.buttonGroup?.getVNode();
	}
}

const pageDialog = new DialogPage();

export { pageDialog, DialogPage };
